package com.qiantu.phone.ui.fragment.device;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.DeviceCustomNewBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.widget.btnview.OnBtnClickListener;
import com.qiantu.phone.widget.btnview.OnBtnLongClickListener;
import com.qiantu.phone.widget.btnview.OneTwoThreeFourSixEightNineButtonView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * desc   : 4键
 */
public final class FourKeyFragment extends KeyDeviceFragment {

    private OneTwoThreeFourSixEightNineButtonView btnView;
    private int clickCount;

    public static FourKeyFragment newInstance() {
        FourKeyFragment fragment = new FourKeyFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_four_key;
    }

    @Override
    protected void initView() {
        super.initView();
        btnView = findViewById(R.id.btn_view);
        btnView.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                getHandler().removeCallbacksAndMessages(null);
                getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        switch(clickCount){
                            case 1:
                            case 2:
                            case 3:
                                operation(btnID, clickCount);
                                break;
                        }
                        clickCount = 0;
                    }
                }, 290);
                clickCount++;
            }
        });
        btnView.setOnBtnLongClickListener(new OnBtnLongClickListener() {
            @Override
            public void onLongClick(int btnID) {
                operation(btnID, 13);
            }
        });
        updateView();
    }

    private void operation(int value, int operation) {
        Map<String, Object> option = new HashMap<>();
        option.put("key_index", value);
        option.put("key_operate", operation);
        Map<String, Object> status = new HashMap<>();
        status.put(STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
//                btnView.setSelectedKey(value, !btnView.isSelected());
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    protected void updateView() {
        super.updateView();
//        if (mDevice.isIsOnline()) {
//            btnView.setEnabled(true);
//        }
//        updateCustomKey(mDevice.getDeviceCustomKeys());
        getDeviceCustomKeyList();
    }

    public void updateCustomKeyNew(List<DeviceCustomNewBean> customKeyList ){
        String defaultName = AppApplication.getInstance().getResources().getString(R.string.custom_name);
        String key1 = defaultName;
        String key2 = defaultName;
        String key3 = defaultName;
        String key4 = defaultName;
        if (customKeyList != null) {
            for (DeviceCustomNewBean keyData : customKeyList) {
                String sub = getBingKeyName(keyData);
                switch (keyData.getKeyIndex()) {
                    case 1:
                        key1 = keyData.getKeyName();
                        if (!sub.isEmpty()){
                            key1 = key1 + "sub" + sub;
                        }
                        break;
                    case 2:
                        key2 = keyData.getKeyName();
                        if (!sub.isEmpty()){
                            key2 = key2 + "sub" + sub;
                        }
                        break;
                    case 3:
                        key3 = keyData.getKeyName();
                        if (!sub.isEmpty()){
                            key3 = key3 + "sub" + sub;
                        }
                        break;
                    case 4:
                        key4 = keyData.getKeyName();
                        if (!sub.isEmpty()){
                            key4 = key4 + "sub" + sub;
                        }
                        break;
                }
            }
        }
        btnView.setText(key1, key2, key3, key4);
    }

}